package com.zygame.sdk.utils.ui;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Observable;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.zygame.sdk.view.BaseView;

/**
 * 显示view的管理工具
 * 
 * @author Administrator
 * 
 */
public class UiManager extends Observable {
//	private static final String TAG = "UiManager";

	private static UiManager instance;

	public static UiManager getInstance() {
		if (instance == null) {
			instance = new UiManager();
		}
		return instance;
	}

	public void clearManager() {
		instance = null;
	}

	private ViewGroup showContiner;

	public void setShowContiner(ViewGroup middleContiner) {
		this.showContiner = middleContiner;
	}
	

	// 利用内存的空间换运行时间
	private Map<String, BaseView> VIEWCACHE = new HashMap<String, BaseView>();// key
																				// :String
																				// 标示当前BaseView的子类
	public BaseView currentView;// 当期之内正在显示

	// View的历史栈。
	private LinkedList<String> HISTROY = new LinkedList<String>();

	public void changeView(Class<? extends BaseView> targetClazz) {
		// 比对：两方（targetClazz VS 当前正在显示）
		if (currentView != null && currentView.getClass() == targetClazz) {
			return;
		}

		// 之前曾经创建过的界面进行存储
		// 判断存储里面是否含有
		// 如果没有，创建，在存储到集合中
		BaseView targetView = null;
		String key = targetClazz.getSimpleName();
		if (VIEWCACHE.containsKey(key)) {
			targetView = VIEWCACHE.get(key);
		} else {  
			try {
				Constructor<? extends BaseView> constructor = targetClazz
						.getConstructor(Context.class);
				targetView = constructor.newInstance(getContext());
				VIEWCACHE.put(key, targetView);

			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		showContiner.removeAllViews();
		View view2 = targetView.getView();
		showContiner.addView(view2);
		// 记录当期正在显示的界面
		currentView = targetView;

		// 添加正在显示的界面到栈顶
		HISTROY.addFirst(key);

		changViewListener();

	}

	public void changeView(Class<? extends BaseView> targetClazz, String url) {
		// 比对：两方（targetClazz VS 当前正在显示）
		if (currentView != null && currentView.getClass() == targetClazz) {
			return;
		}

		// 之前曾经创建过的界面进行存储
		// 判断存储里面是否含有
		// 如果没有，创建，在存储到集合中
		BaseView targetView = null;
		String key = targetClazz.getSimpleName();
		if (VIEWCACHE.containsKey(key)) {
			targetView = VIEWCACHE.get(key);
		} else {
			try {
				Constructor<? extends BaseView> constructor = targetClazz.getConstructor(Context.class);
				targetView = constructor.newInstance(getContext(), url);
				VIEWCACHE.put(key, targetView);

			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		// Log.i(TAG, targetView.toString());

		showContiner.removeAllViews();
		View view2 = targetView.getView();
		showContiner.addView(view2);
		// 记录当期正在显示的界面
		currentView = targetView;

		// 添加正在显示的界面到栈顶
		HISTROY.addFirst(key);

		changViewListener();

	}

	public void changeView(BaseView view) {
		if (currentView != null && currentView.getClass() == view.getClass()) {
			return;
		}
		BaseView targetView = null;
		String key = view.getClass().getSimpleName();
		if (VIEWCACHE.containsKey(key)) {
			targetView = view;
		} else {
			targetView = view;
			VIEWCACHE.put(key, view);
		}

		showContiner.removeAllViews();
		View view2 = targetView.getView();
		showContiner.addView(view2);
		// 记录当期正在显示的界面
		currentView = targetView;
		// 添加正在显示的界面到栈顶
		HISTROY.addFirst(key);
		changViewListener();
	}

	/**
	 * 还用保存到栈里
	 * 
	 * @param targetClazz
	 */
	public void changeViewNoHistroy(Class<? extends BaseView> targetClazz) {
		if (currentView != null && currentView.getClass() == targetClazz) {
			return;
		}
		BaseView targetView = null;
		try {
			Constructor<? extends BaseView> constructor = targetClazz
					.getConstructor(Context.class);
			targetView = constructor.newInstance(getContext());
		} catch (Exception e) {
			e.printStackTrace();
		}
		showContiner.removeAllViews();
		View view2 = targetView.getView();
		showContiner.addView(view2);
		// 记录当期正在显示的界面
		currentView = targetView;
	}
	
	
	/**
	 * 还用保存到栈里
	 * 
	 * @param targetClazz
	 */
	String currentName;
	public void changeViewNoHistroy(String key,BaseView baseView) {
		if (currentView != null && key.equals(currentName)) {
			return;
		}
		BaseView targetView = baseView;
		showContiner.removeAllViews();
		View view2 = targetView.getView();
		showContiner.addView(view2);
		// 记录当期正在显示的界面
		currentName = key;
	}
	
	
	
	/**
	 * 还用保存到栈里
	 * 
	 * @param targetClazz
	 */
	String currentName2;
	public BaseView changeViewNoHistroy2(String key,BaseView baseView) {
		if (currentView != null && key.equals(currentName2)) {
			return currentView;
		}
		
		BaseView targetView = null;
		if (!VIEWCACHE.containsKey(key)) VIEWCACHE.put(key, targetView);
		
		targetView = baseView;
		showContiner.removeAllViews();
		View view2 = targetView.getView();
		showContiner.addView(view2);
		// 记录当期正在显示的界面
		currentView = targetView;
		return currentView;

	}
	
	

	public Context getContext() {
		return showContiner.getContext();
	}

	private void changViewListener() {
		super.setChanged();
		super.notifyObservers(currentView.getId());
	}

	/**
	 * 返回键操作
	 * 
	 * @return
	 */
	public boolean goBack() {
		// addFrist——界面成功切换
		// getFrist
		// removeFrist

		// ②检查是否有历史界面，如果有将历史记录展示
		// 当系统中有多个Activity时，系统是如何处理——栈
		// 获取到栈顶元素
		// 删除栈顶元素
		// 获取到最新的栈顶，进行展示
//		if (currentView.getClass() == MsgView.class
//				|| currentView.getClass() == HomeView.class
//				|| currentView.getClass() == FriendsView.class
//				|| currentView.getClass() == RefinedView.class
//				|| currentView.getClass() == MoreView.class) {
//			return false;
//		}
		if (HISTROY.size() > 0) {
			// 当剩下最后一个元素，不能够删除
			if (HISTROY.size() == 1) {
				showContiner.removeAllViews();
				HISTROY.clear();
				currentView = null;  
				return false;
			}

			HISTROY.removeFirst();

			/**
			 * Throws: NoSuchElementException - if this LinkedList is empty.
			 */
			if (HISTROY.size() > 0) {
				String key = HISTROY.getFirst();

				BaseView targetView = VIEWCACHE.get(key);
				showContiner.removeAllViews();
				// ViewGroup view = (ViewGroup) targetView.getView();
				// if (condition) {
				// }
				showContiner.addView(targetView.getView());
				// 更新当前正在展示的界面
				currentView = targetView;
				return true;
			}
		}
		changViewListener();
		return false;
	}
}
